<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="s" uri="/struts-tags"%> 
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<sx:head cache="false" parseContent="true"/>

<link type="text/css" rel="stylesheet" href="css/reserve.css" media="screen">
<link type="text/css" rel="stylesheet" href="css/standard.css" media="screen">
<link type="text/css" rel="stylesheet" href="css/common.css" media="screen">

<script language="javascript" type="text/javascript" src="js/datevalidate.js"></script>
<script language="javascript" type="text/javascript" src="js/CommonReserve.js"></script>
<script language="javascript" type="text/javascript" src="js/AjaxHandlerUtil.js"></script>
<script language="javascript" type="text/javascript" src="js/NumberFormat154.js"></script>

<script language="javascript">     
window.focus();
if (!window.opener)
	window.document.write();

var openerObj = window.opener;

function saveDiscount(){
	var amount 		= document.getElementById("amount");
	var userName 	= document.getElementById("userName");
	var password 	= document.getElementById("password");
	
    if(amount == ""){
		alert("Please input amount");
		amount();
		return false;
	}else if(userName.value == ""){
		alert("Please input userName");
		userName.focus();
		return false;
	}else if(password.value == ""){
		alert("Please input password");
		password.focus();
		return false;
   	}else{
   		if(confirm("Do you want to save?")){   			
   		    var params		= "discountVO.userName=" + userName.value + "&discountVO.password=" + password.value + "&discountVO.discountAmount=" + amount.value;

   		    document.getElementById("discountIndicator").style.display = "inline";
   		    isdnSendRequest("reservePayment_saveDiscountJSON.action?", params , "POST", callbackSaveDiscount);   	   		
   		}
   	}
	return true;
}

callbackSaveDiscount = function() {
    var ready = req.readyState;
    //  console.debug(ready);
    var jsonObject=null;
    if ( ready == 4 ){
        if(req.status == 200) {
            jsonObject=eval( "("+ req.responseText +")" );
            
			var userId	= document.getElementById("userId");
			var userName= document.getElementById("userName");
			var amount	= document.getElementById("amount");
			var flag	= document.getElementById("authorizedFlag");

			userId.value 	= jsonObject.object.data.userId;
			flag.value	 	= jsonObject.object.data.authorizedFlag;

			if(flag.value == "N"){
				var msg = document.getElementById("msgNoAuthorized").innerText;
				alert(msg);
			}else{
				var opennerForm						= openerObj.frmReservePayment;	
				opennerForm.discountAmt.value		= amount.value;
				opennerForm.authorizedBy.value		= userId.value;
				opennerForm.authorizedByName.value	= userName.value;
				
	            document.getElementById("discountIndicator").style.display = "none";
				openerObj.calculatePaymentTotalAmount();	
				window.close();
			}

            document.getElementById("discountIndicator").style.display = "none";
        }
    }
};
dojo.event.topic.subscribe("/beforeAddDiscount", function(event, widget){
    try{
		var amount 		= document.getElementById("amount");
		var userName 	= document.getElementById("userName");
		var password 	= document.getElementById("password");
		
        if(amount == ""){
			alert("Please input amount");
			amount();
			event.cancel = true;
		}else if(userName.value == ""){
			alert("Please input userName");
			userName.focus();
			event.cancel = true;
		}else if(password.value == ""){
			alert("Please input password");
			password.focus();
			event.cancel = true;
       	}else{
       		if(!confirm("Do you want to save?")){
       			event.cancel = true;
       		}
       	}
    } catch(err){
         alert("Javascript error in this reserve info page beforeAddDiscount main function() [Error: " + err.message + "]");
         event.cancel = true;
    }
});
</script>

<sx:div id="discountDIV" loadingText="Loading..." showLoadingText="true"
		cssStyle="padding-top: 3px; background-image: url(images/top_page_bg.jpg); background-repeat: repeat-x;"
    	parseContent="true" separateScripts="true" executeScripts="true" refreshOnShow="true" cssClass="yui-skin-sam">
   
    <s:form id="frmDiscount" name="frmDiscount" method="post" theme="simple">
        <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%" align="center">
            <tr> 
                <td width="100%" valign="top" style="padding-top: 20;">
                    <table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">
                        <tr> 
                            <td colspan="2">
                                <s:actionerror />
                                <s:actionmessage cssStyle="font: bolder;color: blue;" />
                            </td>
                        </tr>
                        <tr>
							<td align="right" style="color: #FFFFFF; padding-right: 7px">
								<s:label key="reserve.room.discount.amount" /> &nbsp;<font color="red">*</font>
							</td>
							<td>
							   	<s:textfield id="amount" name="discountVO.discountAmount" size="12" maxlength="10"
							   				 cssClass="iNR" onkeypress="return text_OnKeyPress_NumberOnlyAndDot()"/>
							</td>
                        </tr>
                        <tr>
							<td align="right" style="color: #FFFFFF; padding-right: 7px">
								<s:label key="reserve.room.discount.username" /> &nbsp;<font color="red">*</font>
							</td>
							<td>
							   	<s:textfield id="userName" name="discountVO.userName" size="20" cssClass="iNL"/>
							</td>
                        </tr>
                        <tr>
							<td align="right" style="color: #FFFFFF; padding-right: 7px">
								<s:label key="reserve.room.discount.password" /> &nbsp;<font color="red">*</font>
							</td>
							<td>
							   	<s:password id="password" name="discountVO.password" size="21" cssClass="iNL"/>
							</td>
                        </tr>
                        <tr>
                        	<td align="center" colspan="2" style="padding-top: 15px">
							   	<s:hidden id="userId" name="discountVO.userId" />
							   	<s:hidden id="authorizedFlag" name="discountVO.authorizedFlag" />
							   	<s:label key="reserve.error.noauthorized4discount" id="msgNoAuthorized" cssStyle="display:none;" />
							   	<%--sx:submit key="reserve.room.discount.button.ok" indicator="discountIndicator" id="btnSaveDiscount" 
								   targets="discountDIV" href="reservePayment_addDiscount.action" beforeNotifyTopics="/beforeAddDiscount">
								</sx:submit--%>
								<input type="button" value="<s:text name="reserve.room.discount.button.ok" />" onclick="saveDiscount();"
									   style="width: 65; border: 1px solid #222222" /> &nbsp;
								<input type="button" value="<s:text name="reserve.room.discount.button.cancel" />" onclick="window.close()"
									   style="width: 70; border: 1px solid #222222" />
								<img id="discountIndicator" src="${pageContext.request.contextPath}/images/indicator.gif" alt="Loading..." style="display:none"/>
                        	</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </s:form>

<s:if test="discountVO.authorizedFlag == 'Y'">
	<s:url id="refreshReservePaymentURL" value="refreshReservePayment.action?" />
	<sx:div id="refreshReservePaymentDIV" autoStart="false" parseContent="true" executeScripts="true"
	        separateScripts="true" showLoadingText="true"
	        href="%{refreshReservePaymentURL}" refreshOnShow="true"></sx:div>   
</s:if>
                
</sx:div>
